import isEmpty from 'lodash/isEmpty';
import { Button, Form } from 'antd';
import { WeaSwitch } from 'comsMobx';
import { WeaSearchGroup } from 'ecCom';
import { toJS } from 'mobx';

export const getNewFiels = (fields = {}) => {
  const params = {};
  if (!isEmpty(fields)) {
    for (const key in fields) {
      params[fields[key].name] = fields[key].value;
    }
  }
  return params;
};

// 用于获取高级搜索中的表单控件
// 一定要先初始化initFormFields表单对象
export const getSearchs = (form, conditioninfo, callBack) => {
  const { isFormInit } = form;
  const group = [];
  if (isFormInit) {
    const formParams = form.getFormParams(); // 获取表单的值
    const FormItem = Form.Item;
    conditioninfo = toJS(conditioninfo);
    // isFormInit 为true，form已经初始化完成
    isFormInit && conditioninfo.map((c, index) => {
      const items = [];
      c.items.map((fields) => {
        items.push({
          com: (<FormItem
            ecId={`${this && this.props && this.props.ecId || ''}_FormItem@twgji2@${fields.domkey[0]}`}
            label={`${fields.label}`}
            labelCol={{ span: `${fields.labelcol}` }}
            wrapperCol={{ span: `${fields.fieldcol}` }}
          >
            {/* 根据 WeaSwitch 获取对应的组件 */}
            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@vz4qo8@${fields.domkey[0]}`} fieldConfig={fields} form={form} formParams={formParams} onEnterSearch={callBack || ''} />
          </FormItem>),
          colSpan: 1,
        });
      });
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@jnow5l@${index}`} needTigger={false} title={c.title} showGroup={c.defaultshow} items={items} />);
    });
  }
  return group;
};


export const createReplyBottomAtt = (label, list, href) => {
  if (!list || list.length == 0) return null;
  return (
    <div className="car-att-list">
      <div>{label}</div>
      <ul>
        {list.map((item, index) => (
          <li><a href={href(item.id)} target="_blank">{item.name}</a></li>
        ))}
      </ul>
    </div>
  );
};
export const dateFormat = (date, fmt = 'yyyy-MM-dd') => {
  if (date && fmt) {
    let _this = date;
    const type = Object.prototype.toString.call(date);
    if (type == '[object String]') {
      let dateArray = date.split(' '),
        d1 = dateArray[0].split('-'),
        d2 = dateArray[1] ? dateArray[1].split(':') : [];
      _this = new Date((d1[0] || 0) * 1, (d1[1] || 0) * 1 - 1, (d1[2] || 0) * 1, (d2[0] || 0) * 1, (d2[1] || 0) * 1, (d2[2] || 0) * 1);
    } else if (type == '[object Date]') {

    } else {
      return '';
    }
    const o = {
      'M+': _this.getMonth() + 1, // 月份
      'd+': _this.getDate(), // 日
      'h+': _this.getHours() % 12 == 0 ? 12 : _this.getHours() % 12, // 小时
      'H+': _this.getHours(), // 小时
      'm+': _this.getMinutes(), // 分
      's+': _this.getSeconds(), // 秒
      'q+': Math.floor((_this.getMonth() + 3) / 3), // 季度
      S: _this.getMilliseconds(), // 毫秒
    };
    const week = {
      0: '\\u65e5',
      1: '\\u4e00',
      2: '\\u4e8c',
      3: '\\u4e09',
      4: '\\u56db',
      5: '\\u4e94',
      6: '\\u516d',
    };
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (`${_this.getFullYear()}`).substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
      const weekStr = ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\\u661f\\u671f' : '\\u5468') : '') + week[`${_this.getDay()}`];
      fmt = fmt.replace(RegExp.$1, Utils.GB2312UnicodeConverter.ToGB2312(weekStr));
    }
    for (const k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length)));
      }
    }
    return fmt;
  }
  return '';
};
